//
// Created by wxd on 2025/7/14.
//

#include "EBO.h"
#include "../global/EngineDataProperties.h"
#include "../wrapper/monitor/GLDebug.hpp"

EBO::EBO(const std::vector<unsigned int> &data) {
    m_Usage = GL_STATIC_DRAW;
    m_Indices = std::make_unique<std::vector<unsigned int>>(data);
    m_EBO = 0;

    GL_CALL(glGenBuffers(1, &m_EBO))
    GL_CALL(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_EBO))
    GL_CALL(glBufferData(GL_ELEMENT_ARRAY_BUFFER, uintSize(m_Indices->size()), m_Indices->data(), m_Usage))
}

EBO::EBO(const std::vector<unsigned int> &data, GLenum usage) {
    m_Usage = usage;
    m_Indices = std::make_unique<std::vector<unsigned int>>(data);
    m_EBO = 0;

    GL_CALL(glGenBuffers(1, &m_EBO))
    GL_CALL(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_EBO))
    GL_CALL(glBufferData(GL_ELEMENT_ARRAY_BUFFER, uintSize(m_Indices->size()), m_Indices->data(), m_Usage))
}

EBO::~EBO() {
    if (m_Indices) {
        m_Indices->clear();
    }

    destroy();
}

void EBO::bind() const {
    GL_CALL(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_EBO))
}

void EBO::unbind() const {
    GL_CALL(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0))
}

void EBO::bindData() const {
    GL_CALL(glBufferData(GL_ELEMENT_ARRAY_BUFFER, uintSize(m_Indices->size()), m_Indices->data(), m_Usage))
}

GLuint EBO::getEBO() const {
    return m_EBO;
}

void EBO::destroy() {
    if (m_EBO != 0) {
        glDeleteBuffers(1, &m_EBO);
        m_EBO = 0;
    }
}






